const asarUtil = require('../../common/utils/asarUtil');
const pluginWarper = require('../warper/pluginWarper');
const searchWarper = require('../warper/searchWarper');
const pkgUtil = require('../../common/utils/pkgUtil');
const uuidUtil = require('../../common/utils/uuidUtil');
const pluginInfoDAO = require('../../core/rdsService/pluginInfoDAO');





exports.installPackage = function (request, event) {

    const path = request.subInputDetail.path
    const pkg = pkgUtil.getPkgByAsarPath(path)

    const groupId = pkg.groupId
    const artifactId = pkg.artifactId
    const version = pkg.version





    pluginInfoDAO.getPluginInfoByCondition(null, groupId, artifactId, (thePlugins) => {
        let thePlugin = null
        if (thePlugins.length > 0) {
            thePlugin = thePlugins[0]
        }
        const uuid = uuidUtil.uuid()
        if (thePlugin == null) {
            thePlugin = pluginInfoDAO.composeModel(uuid, groupId, artifactId, version, pkg)
            pluginInfoDAO.insert(thePlugin)
        } else {
            thePlugin.version = version
            thePlugin.pluginInfo = pkg
            pluginInfoDAO.update(thePlugin)
        }
        asarUtil.movePackage(path, uuid)
    })


}